#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.64])
AC_INIT([run2], [0.4.0], [cygwin@cygwin.com])
AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_SRCDIR([src/run2_main.c])
AC_CONFIG_HEADER([config.h])
AM_INIT_AUTOMAKE([1.11 -Wall -Werror foreign dist-xz silent-rules no-dependencies subdir-objects])
AM_MAINTAINER_MODE
AM_SILENT_RULES

AC_CANONICAL_HOST
AC_CHECK_PROGS([WINDRES],[windres])
if test "x$WINDRES" = "x" ; then
    AC_MSG_ERROR([No windows resource compiler found.  Cannot build.])
fi

VERSION_MAJOR=`echo $VERSION | cut -f1 -d.`
VERSION_MINOR=`echo $VERSION | cut -f2 -d.`
VERSION_PATCH=`echo $VERSION | cut -f3 -d.`

AC_SUBST(VERSION_MAJOR)
AC_SUBST(VERSION_MINOR)
AC_SUBST(VERSION_PATCH)


# enable options
AC_ARG_ENABLE(
  [silent],
  AS_HELP_STRING(
    [--enable-silent],
    [operation is completely silent (default)]),
  [cx_enable_silent=$withval],
  [cx_enable_silent=yes])
if test x"$cx_enable_silent" = xyes ; then
  AC_DEFINE([SILENT],1,[Define this for completely silent operation])
fi

AC_ARG_ENABLE(
  [gui],
  AS_HELP_STRING(
    [--enable-gui],
    [allow use of MSWin popup windows for messages (default)]),
  [cx_enable_gui=$withval],
  [cx_enable_gui=yes])
if test x"$cx_enable_gui" = xyes ; then
  AC_DEFINE([ENABLE_GUI],1,[Define this to 1 to allow MSWin popup windows])
else
  AC_DEFINE([ENABLE_GUI],0)
fi

AC_ARG_ENABLE(
  [tty],
  AS_HELP_STRING(
    [--enable-tty],
    [allow use of TTY console for messages (default)]),
  [cx_enable_tty=$withval],
  [cx_enable_tty=yes])
if test x"$cx_enable_tty" = xyes ; then
  AC_DEFINE([ENABLE_TTY],1,[Define this to 1 to allow TTY console messages])
else
  AC_DEFINE([ENABLE_TTY],0)
fi

AC_ARG_ENABLE(
  [debug],
  AS_HELP_STRING(
    [--enable-debug],
    [turn on debugging messages]),
  [cx_enable_debug=$withval],
  [cx_enable_debug=no])
if test x"$cx_enable_debug" = xyes ; then
  AC_DEFINE([ENABLE_DEBUG],1,[Define this enable debugging messages])
fi

if test x"$cx_enable_tty" != xyes ; then
  if test x"$cx_enable_gui" != xyes ; then
    AC_MSG_ERROR([At least one of --enable-gui and --enable-tty must be "yes"])
  fi
fi

# Checks for programs.
AC_PROG_CC
AM_PROG_CC_C_O
AC_PROG_INSTALL

LT_INIT([win32-dll])

# Checks for libraries.
AC_SEARCH_LIBS([dlopen], [dl], [],
	AC_MSG_ERROR([Require dlopen]))

ACX_PTHREAD
AM_PATH_XML2
AC_CHECK_LIB([ustr], [ustr_init_alloc], [],
	AC_MSG_ERROR([Require libustr]))

# Checks for header files.
AC_PATH_X
AC_HEADER_STDC
AC_CHECK_HEADERS([locale.h malloc.h stdlib.h math.h string.h assert.h pwd.h])
AC_CHECK_HEADERS([getopt.h X11/Xlib.h dlfcn.h sys/cygwin.h windows.h])
AC_CHECK_HEADER([ustr.h],
                [AC_DEFINE([HAVE_USTR_H], [1],
                  [Define to 1 if you have <ustr.h>.])],
                [AC_MSG_ERROR([<ustr.h> is required.])])

# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_SIZE_T

# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_STRTOD
AC_FUNC_VPRINTF
AC_CHECK_FUNCS([clock_gettime floor setlocale strdup setenv])
AC_CHECK_FUNC([getopt_long], [], [replace_getopt=yes])
AM_CONDITIONAL([REPLACE_GETOPT], [test "$replace_getopt" = yes])
if test "$replace_getopt" = yes; then
      AC_DEFINE([REPLACE_GETOPT], 1,
        [Define if you must use the replacement getopt_long implementation])
fi

dnl should only exist on windows, I hope
AC_CHECK_STDCALL_FUNC([OpenClipboard],[void *])
if test x"$ac_cv_func_OpenClipboard" = xyes ; then
    AC_DEFINE([HAVE_OPENCLIPBOARD], 1,
	[Define if you have the __stdcall__ OpenClipboard function])
fi

AH_BOTTOM([
#ifndef ENABLE_GUI
# define ENABLE_GUI 1
#endif
#ifndef ENABLE_TTY
# define ENABLE_TTY 1
#endif
#if (ENABLE_TTY == 0) && (ENABLE_GUI == 0)
# error At least one of ENABLE_GUI and ENABLE_TTY must be 1
#endif
])

AC_CONFIG_FILES([Makefile
                 cdsl/Makefile
                 lib/Makefile
                 src/Makefile
                 src/run2_res.rc
                 test/Makefile])
AC_OUTPUT

